package com.example.smartagrisys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartagrisys.entity.FertilizerInventory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 肥料库存Mapper接口
 */
@Mapper
public interface FertilizerInventoryMapper extends BaseMapper<FertilizerInventory> {
    
    /**
     * 获取农户的肥料库存列表（关联肥料表获取名称）
     */
    @Select("SELECT fi.*, f.name AS fertilizerName, f.type AS fertilizerType " +
            "FROM fertilizer_inventory fi " +
            "LEFT JOIN fertilizer f ON fi.fertilizer_id = f.id " +
            "WHERE fi.farmer_id = #{farmerId} AND fi.deleted = 0")
    List<Object> getFertilizerInventoryWithDetails(@Param("farmerId") Long farmerId);
    
    /**
     * 检查农户是否有足够的肥料库存
     */
    @Select("SELECT COUNT(*) FROM fertilizer_inventory " +
            "WHERE farmer_id = #{farmerId} AND fertilizer_id = #{fertilizerId} " +
            "AND quantity >= #{requiredQuantity} AND deleted = 0")
    int checkFertilizerInventory(@Param("farmerId") Long farmerId, 
                                @Param("fertilizerId") Long fertilizerId, 
                                @Param("requiredQuantity") Double requiredQuantity);
} 